package com.iyao_app.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;

import com.iyao_app.MyApplication;
import com.iyao_app.R;
import com.iyao_app.entry.User;
import com.iyao_app.mvp.home.MainActivity;
import com.iyao_app.util.DbUtil;
import com.iyaowoqu.base_library.util.LogUtil;
import com.mingle.widget.ShapeLoadingDialog;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.auth.AuthService;
import com.netease.nimlib.sdk.auth.LoginInfo;

/**
 * 作者 : 胡家兵
 * 时间 : 2017/11/9.
 */

public class BaseActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        MyApplication.getInstance().addActivity(this);
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void onDestroy() {
        MyApplication.getInstance().removeActivity(this);
        super.onDestroy();
    }

    // 硬按键
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onBackPressed();
            return false;
        }
        return false;
    }

    public void onBackPressed() {
        closeDialog();
        if (this instanceof MainActivity) {
            ((MainActivity) this).exitBy2Click();
        } else {
            finish();
        }
    }

    /**
     * 云信的登录
     */
    public void loginYun() {
        NIMClient.getService(AuthService.class).login(loginInfo())
                .setCallback(new RequestCallback() {
                    @Override
                    public void onSuccess(Object o) {
                        LogUtil.e("云信登录成功" + o);
                    }

                    @Override
                    public void onFailed(int i) {
                        LogUtil.e("aa","token" + DbUtil.getUser().token);
                        LogUtil.e("云信登录失败：" + i);
                    }

                    @Override
                    public void onException(Throwable throwable) {
                        LogUtil.e("aa","token" + DbUtil.getUser().token);
                        LogUtil.e("云信登录异常" + throwable);
                    }
                });
    }

    private LoginInfo loginInfo() {

        User user = DbUtil.getUser();
        if (user == null) return null;
        return new LoginInfo(DbUtil.getUser().id, DbUtil.getUser().token);
    }

    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.noanim, R.anim.exit_leftttoight);
    }

    ShapeLoadingDialog shapeLoadingDialog;

    public void showDialog() {
        LogUtil.e("====showDialog=");
        shapeLoadingDialog = new ShapeLoadingDialog.Builder(this)
                .loadText("加载中...")
                .cancelable(false)
                .build();
        shapeLoadingDialog.show();
    }

    public void closeDialog() {
        if (shapeLoadingDialog != null)
            shapeLoadingDialog.dismiss();
        shapeLoadingDialog = null;
    }
}
